package com.fuhao.topic;

import com.fuhao.utils.RabbitUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * topic模型中消费者模糊绑定路由键，可以模糊匹配到生产者路由键
 *
 * @author zhang
 */
public class comsumer1 {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = RabbitUtils.getConnection();
        final Channel channel = connection.createChannel();
        channel.queueDeclare("exchange_topic_queue1", true, false, false, null);
        channel.queueBind("exchange_topic_queue1", "exchange_topic", "*.item.*");
        channel.basicQos(1);
        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg = new String(body, "UTF-8");
                System.out.println("topic模型消费者1接收到消息：" + msg);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    // 手动回执
                    channel.basicAck(envelope.getDeliveryTag(), false);
                    }
                }
            }

            ;
        channel.basicConsume("exchange_topic_queue1",false,consumer);
        }
    }
